/*
################################################################################################################################# Included
0 9    B  Allow/prevent seeing the names of the people around : @shownames=<y/n>     
1 9    B  Allow/prevent seeing all the hovertexts : @showhovertextall=<y/n>     
2 9    B  Allow/prevent seeing the hovertexts on the HUD of the user : @showhovertexthud=<y/n>     
3 9    B  Allow/prevent seeing the hovertexts in-world : @showhovertextworld=<y/n>
#################################################################################################################################
*/
integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
list gMenu=[];
list statesMenu=[0,0,0,0];
list gAvatarAllow;
key ToucherID;
integer gMenuLink =1337;
integer gDemon =1338;
integer gPlugin =1339;
integer gVic =1329;
integer channel_dialog;
integer listen_id;
string msg ="Hover Test";
list sideMenu=[" ","Back"," "];
list objects=[];
list selected=[];
integer gPage;


list pageMenu(list in ,integer page)
{
	list out;
	integer maxpages =llGetListLength(in)/9;
	page = (page)%(maxpages+1);
	out =["<<","Back",">>"];
	integer _listcount =(page *9)+9;
	integer _i=_listcount-9;
	
	for (_i;_i<_listcount;_i++)
	{
		if(_i<=llGetListLength(in)&&llList2String(in,_i)!="")
		out+=llList2String(in,_i);
	}
	return out;
}

Debug(string _text)
{
	llSay(DEBUG_CHANNEL, _text);
}

llRlvSay(string command)
{
	if(attachment==TRUE)
	{
		llOwnerSay("@"+command);
	}
	else
	{
		llSay(relaychannel,cmdname + "," + (string)victim  + "," + "@"+command);
	}
}

MakeRLVState()
{
	if(attachment==TRUE)
	{
		//onRez we make this to be secure if its an attachmentttt
		if(llList2Integer(statesMenu,0)==1)llRlvSay("shownames=n");
		if(llList2Integer(statesMenu,1)==1)llRlvSay("showhovertextall=n");
		if(llList2Integer(statesMenu,2)==1)llRlvSay("showhovertexthud=n");
		if(llList2Integer(statesMenu,3)==1)llRlvSay("showhovertextworld=n");
	}
}

llLRP(integer element_new, integer placeinlist)
{
	statesMenu = llListReplaceList(statesMenu, [element_new], placeinlist, placeinlist);
}

makeMenu()
{
	gMenu=[];
	objects=[];
	integer x=0;
	string name="";
	while(x< llGetInventoryNumber(INVENTORY_OBJECT))
	{
		//if(msg == llGetSubString(name, 0, 23))
		name =llGetInventoryName(INVENTORY_OBJECT, x);
		if(name == llGetSubString(name, 0, 23))
		{
			objects+=name;
		}
		x++;
	}
	x=0;
	while(x< llGetInventoryNumber(INVENTORY_CLOTHING))
	{
		//if(msg == llGetSubString(name, 0, 23))
		name =llGetInventoryName(INVENTORY_CLOTHING, x);
		if(name == llGetSubString(name, 0, 23))
		{
			objects+=name;
		}
		x++;
	}
	if(llGetListLength(objects)<10)
	{
		objects= ["Clear","Back", "Attach"]+objects;
	}
	else
	{
		objects+=["Clear","Attach"];
		objects= pageMenu(objects,gPage);
	}
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,objects, channel_dialog);
	llSetTimerEvent(45.0);
}

integer randBetween(integer min, integer max)
{
    return (integer)(llFrand((float)(max - min)) + min);
}

Rezz()
{
	if(llGetListLength(objects)>0)
	{
		list inventory=objects;
		string text ="";
		string text2 ="";
		integer add = randBetween(10,1000);
		text = "#RLV/~DV"+(string)add;
		text2= "~DV"+(string)add;
		llRlvSay("acceptpermission=add");
		llSleep(5.0);
		if(attachment==TRUE)
		{
			llGiveInventoryList(llGetOwner(), text, inventory);
		}
		else
		{
			llGiveInventoryList(victim, text, inventory);
		}
		llSleep(10.0);
		llRlvSay("acceptpermission=rem");
		llRlvSay("attachall:"+text2+"=force");
	}
}

list ListUnique( list lAll ) {
    integer i;
    list lFiltered = llList2List(lAll, 0, 0);
    integer iAll = llGetListLength( lAll );
    for (i = 1; i < iAll; ++i) {
        if ( llListFindList(lFiltered, llList2List(lAll, i, i) ) == -1 ) {
            lFiltered += llList2List(lAll, i, i);
        }
    }
    return lFiltered;
}


default {
    state_entry() 
    {
    	    llAllowInventoryDrop(TRUE); 
    }
    
    listen(integer channel, string name, key id, string message)
    {
		llListenRemove(listen_id);
		llSetTimerEvent(0.0);


		
		if(message=="<<")
	  	{
	  		gPage--;
			integer maxpages =llGetListLength(objects)/9;
			if(gPage<0)
			{
				gPage = maxpages;
			}
			makeMenu();
	  	}
	  	else if(message==">>")
	  	{
	  		gPage++;
	  		makeMenu();
	  	}
	  	else if(message=="Clear")
	  	{
	  		selected=[];
	  		makeMenu();
	  	}
	  	else if(message=="Attach")
	  	{
	  		Rezz();
	  		makeMenu();
	  	}
	  	else if(message=="Back")llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_RLV", ToucherID);
	  	else if(~llListFindList(objects, (list)message))
	  	{
	  		selected+=message;
	  		selected=ListUnique(selected);
	  		msg = llList2CSV(selected);
	  		makeMenu();
	  		
	  	}
		
    }
    
    on_rez(integer on)
    {
    	llAllowInventoryDrop(TRUE);
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				makeMenu();
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str =="attachment")attachment=TRUE;
  			else if(str =="device")attachment=FALSE;
  			else if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);

  			}
  		}
  		if(num==gPlugin)
  		{
  			if(str=="ON_REZ")
  			{
  				MakeRLVState();
  			}
  			if(str=="RESET")
  			{
  				statesMenu=[0,0,0,0];
  			}
  		}
  		if(num==gVic)
  		{
  				victim=(key)str;
  				cmdname = (string)llGetKey();
  		}
  	}
  	
  	timer()
    {
    	llSetTimerEvent(0.0);
    	llListenRemove(listen_id);
    }
}
